package com.yang.strutspractice.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;
import com.yang.strutspractice.user.User;

public class UserAction extends ActionSupport {
	private User user;
	
	private InputStream downloadStream;


	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String select(){
		System.out.println(user.getUsername());
		
		System.out.println(user.getAge());
		
		return "success";
		
	}
	
	public String delete(){
		System.out.println(user.getUsername());
		
		System.out.println(user.getAge());
		
		return "success";
	}
	
	public String testResultType(){
		
		return SUCCESS;
	}
	
public String testJson(){
		
		return SUCCESS;
	}
	
	/**
	 * 在action调用任何方法前执行
	 * 
	 * 因此如不通过校验，拦截器中会返回"input",
	 * struts.xml中如无相应的result设置，会出现错误
	 * @throws FileNotFoundException 
	 */
//	@Override


//	public void validate(){
//		
//		if(user.getUsername()==null || user.getUsername().length()<6){
//			
//			addFieldError("user.username", "用户名不能为空或低于六个字节");
//			
//		}
//	}

	public String download(){
		
		try {
			
			this.downloadStream = new FileInputStream("i:/bootstrap(simple version).zip");
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return SUCCESS;
	}
	
	
	public InputStream getDownloadStream() {
		return downloadStream;
	}

	public void setDownloadStream(InputStream downloadStream) {
		this.downloadStream = downloadStream;
	}

	/**
	 * 在 action调用select()前执行
	 * 
	 * 如不通过校验，拦截器中会返回"input",
	 * 因此struts.xml中如无相应的result设置，会出现错误
	 * 
	 */


	public void validateDelete(){
		
		if(user.getUsername()==null || user.getUsername().length()<6){
			
			addFieldError("User.Username", "输入的用户名不符合命名规则");
			
		}
	}

}
